package logic

import (
	"context"
	"wxapi/internal/services"

	"wxapi/internal/svc"
	"wxapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetSystemNotifyLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetSystemNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSystemNotifyLogic {
	return &GetSystemNotifyLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetSystemNotifyLogic) GetSystemNotify(req *types.GetSystemNotifyReq) (resp *types.GetSystemNotifyResp, err error) {
	userId, err := services.GetUserIdBySession(l.ctx, l.svcCtx, req.Session)
	if err != nil {
		return nil, err
	}

	list, err := services.GetSystemNotify(l.ctx, l.svcCtx, userId)
	if err != nil {
		return nil, err
	}

	resp = &types.GetSystemNotifyResp{
		Notify: make([]types.SystemNotify, 0, len(list)),
	}
	for _, v := range list {
		resp.Notify = append(resp.Notify, types.SystemNotify{
			ID:      v.ID,
			Title:   v.Title,
			Content: v.Content,
		})
	}
	return
}
